package blockchain.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NetPortScanner {
    private final String subnet;
    private final int port;
    private final int threadCount;
    private final int timeout;
    public NetPortScanner(String subnet, int port, int threadCount, int timeout) {
        this.subnet = subnet;
        this.port = port;
        this.threadCount = threadCount;
        this.timeout = timeout;
    }
    public NetPortScanner(String subnet, int port) {
        this(subnet, port, 25, 50);
    }
    public NetPortScanner() {
        this(BlockChainConfig.subnet, BlockChainConfig.port, 25, 50);
    }
    public ArrayList<Integer> Run() {
        ArrayList<Integer> allPortIp = new ArrayList<>();
        try  {
            ExecutorService executor = Executors.newFixedThreadPool(threadCount);
            for (int i = 1; i <= 254; i++) {
                final int ipPart = i;
                executor.submit(() -> {
                    String ip = subnet + ipPart;
                    try (Socket socket = new Socket()) {
                        socket.connect(new InetSocketAddress(ip, port), timeout);
                        System.out.println("Port " + port + " is open on " + ip);
                        socket.close();
                        allPortIp.add(Integer.parseInt(ip));
                    } catch (IOException e) {
                        // 连接失败，端口可能关闭或存在网络问题
                    }
                });
            }
            executor.shutdown();
        } catch (Exception e) {
            System.out.println("Scanner error: " + e.getMessage());
        }
        return allPortIp;
    }
    public ArrayList<Integer> Run2() {
        ArrayList<Integer> allPortIp = new ArrayList<>();
        try  {
            ExecutorService executor = Executors.newFixedThreadPool(threadCount);
            for (int j = 1; j <= 254; j++){
                for (int i = 1; i <= 254; i++) {
                    final int ipPartI = i;
                    final int ipPartJ = j;
                    executor.submit(() -> {
                        String ip = subnet + ipPartI + "." + ipPartJ;
                        try (Socket socket = new Socket()) {
                            socket.connect(new InetSocketAddress(ip, port), timeout);
                            System.out.println("Port " + port + " is open on " + ip);
                            socket.close();
                            allPortIp.add(Integer.parseInt(ip));
                        } catch (IOException e) {
                            // 连接失败，端口可能关闭或存在网络问题
                        }
                    });
                }
            }
            executor.shutdown();
        } catch (Exception e) {
            System.out.println("Scanner error: " + e.getMessage());
        }
        return allPortIp;
    }
    public static void main(String[] args) {
        NetPortScanner netPortScanner = new NetPortScanner("172.16.76.", 3389, 50, 100);
        ArrayList<Integer> allPorts = netPortScanner.Run();
        //NetPortScanner netPortScanner2 = new NetPortScanner("169.254.", 80, 50, 10);
        //ArrayList<Integer> allPorts2 = netPortScanner2.Run2();

    }
}